﻿using System.Security.Cryptography;
using System.Text;

namespace EdgeTTS.Net.Utils;

internal static class Utilities
{
    public static string GenerateSecMsGecToken()
    {
        // 来自 https://github.com/rany2/edge-tts/issues/290#issuecomment-2464956570
        var ticks = DateTime.Now.ToFileTimeUtc();
        ticks -= ticks % 3_000_000_000;
        var str = ticks + "6A5AA1D4EAFF4E9FB37E23D68491D6F4";
        return ToHexString(HashData(Encoding.ASCII.GetBytes(str)));
    }

    private static string ToHexString(byte[] byteArray)
    {
        return BitConverter.ToString(byteArray).Replace("-", "").ToUpper();
    }

    private static byte[] HashData(byte[] data)
    {
        using var sha256 = SHA256.Create();
        var hashBytes = sha256.ComputeHash(data);
        return hashBytes;
    }
}